﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace BUS
{
    public class CRunning
    {

        private CMyHook _Hook;
        private Boolean _Enable;
        private Boolean _IsRuning;
        private IntPtr _AppHandle;
        private Thread _Run;
        private DTO.CKeys _RunKey;
        private BUS.CSendKeys _SendKey = new CSendKeys();
        private int _Loop;
        private System.Collections.ArrayList _Keyup;

        public CRunning(IntPtr handle)
        {
            _Hook = new CMyHook(false, true);
            _Enable = true;
            _IsRuning = false;
            _AppHandle = handle;
            _Loop = 0;
            _Keyup = new System.Collections.ArrayList();
        }

        public void Start()
        {
            _Enable = true;
            _Hook.HKeyDown+=new KeyEventHandler(KeyDown);
            _Hook.HKeyUp+=new KeyEventHandler(KeyUp);
            _Hook.HKeyPress += new KeyPressEventHandler(KeyPress);
            _Run = new Thread(new ThreadStart(Runing));
        }

        private string _Control;
        private string _Alt;
        private string _Shift;
        private void KeyDown(object sender, KeyEventArgs e)
        {
            if (_AppHandle != DTO.CWindowsAPI.GetForegroundWindow())
            {
                if (_SendKey.KeyDownNext == e.KeyValue)
                {
                    _SendKey.KeyDownNext = -1;
                }
                else
                {
                    if (_Control == null && (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey))
                    {
                        _Control = CTransferKeysCode.SwithText((int)Keys.ControlKey) + " + ";
                    }
                    else if (_Alt == null && (e.KeyCode == Keys.LMenu || e.KeyCode == Keys.RMenu))
                    {
                        _Alt = CTransferKeysCode.SwithText((int)Keys.Menu) + " + ";
                    }
                    else if (_Shift == null && (e.KeyCode == Keys.LShiftKey || e.KeyCode == Keys.RShiftKey))
                    {
                        _Shift = CTransferKeysCode.SwithText((int)Keys.ShiftKey) + " + ";
                    }
                    if (e.KeyCode != Keys.LShiftKey && e.KeyCode != Keys.RShiftKey && e.KeyCode != Keys.LControlKey && e.KeyCode != Keys.RControlKey && e.KeyCode != Keys.LMenu && e.KeyCode != Keys.RMenu)
                    {
                        string _temp = _Control + _Shift + _Alt + CTransferKeysCode.SwithText((int)e.KeyValue);
                        if (_Enable)
                        {
                            if (BUS.COption.EnOrDisApp == _temp)
                            {
                                Disable();
                            }
                            else if (_IsRuning)
                            {
                                if (BUS.COption.StopRunning == _temp)
                                {
                                    _Loop = 0;
                                }
                            }
                            else
                            {
                                _RunKey = BUS.CConnect.LayKey(_temp);
                                if (_RunKey.Shortcutkey != null)
                                {
                                    e.Handled = true;
                                    _IsRuning = true;
                                    _Run = new Thread(new ThreadStart(Runing));
                                    _Run.Start();
                                }
                            }
                        }
                        else
                        {
                            if (BUS.COption.EnOrDisApp == _temp)
                            {
                                _Enable = true;
                            }
                        }
                    }
                    if (_IsRuning)
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        _Keyup.Add(e.KeyValue);
                    }
                }
            }
        }

        private void KeyUp(object sender, KeyEventArgs e)
        {
            if (_AppHandle != DTO.CWindowsAPI.GetForegroundWindow())
            {
                if (e.KeyValue == _SendKey.KeyUpNext)
                {
                    _SendKey.KeyUpNext = -1;
                }
                else
                {
                    if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey)
                    {
                        _Control = null;
                    }
                    else if (e.KeyCode == Keys.LShiftKey || e.KeyCode == Keys.RShiftKey)
                    {
                        _Shift = null;
                    }
                    else if (e.KeyCode == Keys.LMenu || e.KeyCode == Keys.RMenu)
                    {
                        _Alt = null;
                    }
                    if (_IsRuning)
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        _Keyup.Remove(e.KeyValue);
                    }
                }
            }
        }

        public void KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void Runing()
        {
            _SendKey.SendKeyUp(_RunKey.Shortcutkey);
            _Keyup.Clear();
            for (_Loop = _RunKey.Loop; _Loop != 0; _Loop--)
            {
                _SendKey.SendKey(_RunKey);
                if (_Loop == 0)
                    break;
                else
                    Thread.Sleep(_RunKey.Delay);
            }
            _IsRuning = false;
        }

        public void Disable()
        {
            _Run.Abort();
            _Keyup.Clear();
            _Enable = false;
            _IsRuning = false;
        }

        public void Enable()
        {
            _Enable = true;
        }
    }
}
